package com.googlecode.icecube.feedback;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.googlecode.icefusion.ui.commons.BackingBeanForm;
import com.googlecode.icefusion.ui.commons.dialog.ErrorDialog;
import com.googlecode.icefusion.ui.commons.dialog.IErrorDialog;
import com.googlecode.icefusion.ui.commons.dialog.IMessageDialog;
import com.googlecode.icefusion.ui.commons.dialog.IQuestionDialog;
import com.googlecode.icefusion.ui.commons.dialog.MessageDialog;
import com.googlecode.icefusion.ui.commons.dialog.QuestionDialog;

/**
 * Manages popup dialog examples.
 * 
 * @author Rainer Eschen
 *
 */
public class PopupDialog extends BackingBeanForm implements IMessageDialog, IErrorDialog, IQuestionDialog {

	private static final long serialVersionUID = -403849047469528352L;

	@Autowired
	@Qualifier("messageDialog")  
	private MessageDialog messageDialog;
	
	@Autowired
	@Qualifier("questionDialog")  
	private QuestionDialog questionDialog;
	
	@Autowired
	@Qualifier("errorDialog")  
	private ErrorDialog errorDialog;
	
	/**
	 * Used to show which button of the question dialog was clicked
	 */
	private String buttonClickedMessage;
	
	/**
	 * Resource bundle id for text in error dialog.
	 */
	private String ErrorText;
	
	/**
	 * true: The question button uses different button texts.
	 */
	private Boolean otherQuestionButtons;
	
	/**
	 * The MessageDialog event is handled by this bean
	 */
	private IMessageDialog messageDialogHandler;
	
	/**
	 * The ErrorDialog event is handled by this bean
	 */
	private IMessageDialog errorDialogHandler;

	/**
	 * Opens a MessageDialog.
	 * @return navigation id
	 */
	public String showMessage () {
		
		this.setMessageDialogHandler(this.messageDialog);
		this.messageDialog.setShow(true);
		return null;
	}

	/**
	 * Opens a MessageDialog which events are handled
	 * by this bean.
	 * @return navigation id
	 */
	public String showMessageCustomHandler () {
		
		this.setMessageDialogHandler(this);
		this.messageDialog.setShow(true);
		return null;
	}
	
	/**
	 * Opens a QuestionDialog.
	 * @return navigation id
	 */
	public String showQuestion () {
		
		this.setOtherQuestionButtons(false);
		this.questionDialog.setShow(true);
		return null;
	}

	/**
	 * Opens a QuestionDialog with other buttons.
	 * @return navigation id
	 */
	public String showQuestionOtherButtons () {
		
		this.setOtherQuestionButtons(true);
		this.questionDialog.setShow(true);
		return null;
	}

	/**
	 * Opens a simple ErrorDialog.
	 * @return navigation id
	 */
	public String showError () {
		
		this.setErrorDialogHandler(this.errorDialog);
		this.setErrorText("application.menu.feedback.popupDialog.errorDialog.text");
		this.errorDialog.setException(null);
		this.errorDialog.setShow(true);
		return null;
	}

	/**
	 * Opens an ErrorDialog with stacktrace viewer.
	 * @return navigation id
	 */
	public String showErrorException () {
		
		try {
			// Simulation of an exception for stacktrace presentation
			throw new NullPointerException();
		}
		catch (NullPointerException e) {
			this.setErrorDialogHandler(this.errorDialog);
			this.setErrorText("application.menu.feedback.popupDialog.errorDialog.exception.text");
			this.errorDialog.setException(e);
			this.errorDialog.setShow(true);
		}
		return null;
	}

	/**
	 * Opens an ErrorDialog which events are handled
	 * by this bean.
	 * @return navigation id
	 */
	public String showErrorCustomHandler () {
		
		this.setErrorDialogHandler(this);
		this.setErrorText("application.menu.feedback.popupDialog.errorDialog.customHandler.text");
		this.errorDialog.setException(null);
		this.errorDialog.setShow(true);
		return null;
	}
	
	/**
	 * YES button management.
	 * @return navigation id (keep page)
	 */
	public String questionDialogButtonYes() {
		if (this.getOtherQuestionButtons()) {
			this.setButtonClickedMessage("application.menu.feedback.popupDialog.questionDialog.otherButtons.result.yes");
		}
		else {
			this.setButtonClickedMessage("application.menu.feedback.popupDialog.questionDialog.result.yes");
		}
		this.questionDialog.setShow(false);
		return null;
	}

	/**
	 * NO button management.
	 * @return navigation id (keep page)
	 */
	public String questionDialogButtonNo() {
		if (this.getOtherQuestionButtons()) {
			this.setButtonClickedMessage("application.menu.feedback.popupDialog.questionDialog.otherButtons.result.no");
		}
		else {
			this.setButtonClickedMessage("application.menu.feedback.popupDialog.questionDialog.result.no");
		}
		this.questionDialog.setShow(false);
		return null;
	}

	public String getButtonClickedMessage() {
		return buttonClickedMessage;
	}

	public void setButtonClickedMessage(String buttonClickedMessage) {
		this.buttonClickedMessage = buttonClickedMessage;
	}

	public String getErrorText() {
		return ErrorText;
	}

	public void setErrorText(String errorText) {
		ErrorText = errorText;
	}

	public Boolean getOtherQuestionButtons() {
		return otherQuestionButtons;
	}

	public void setOtherQuestionButtons(Boolean otherQuestionButtons) {
		this.otherQuestionButtons = otherQuestionButtons;
	}

	/**
	 * Text to show in YES button of question dialog.
	 * @return button text
	 */
	public String getYesButtonText() {
		if (this.getOtherQuestionButtons()) {
			return "application.menu.feedback.popupDialog.questionDialog.otherButtons.yes";
		}
		else {
			return "application.menu.feedback.popupDialog.questionDialog.yes";
		}
	}

	/**
	 * Text to show in NO button of question dialog.
	 * @return button text
	 */
	public String getNoButtonText() {
		if (this.getOtherQuestionButtons()) {
			return "application.menu.feedback.popupDialog.questionDialog.otherButtons.no";
		}
		else {
			return "application.menu.feedback.popupDialog.questionDialog.no";
		}
	}
	
	public String getQuestionText() {
		
		if (this.getOtherQuestionButtons()) {
			return "application.menu.feedback.popupDialog.questionDialog.otherButtons.text";
		}
		else {
			return "application.menu.feedback.popupDialog.questionDialog.text";
		}
	}

	public void setMessageDialogHandler(IMessageDialog messageDialogHandler) {
		this.messageDialogHandler = messageDialogHandler;
	}

	public IMessageDialog getMessageDialogHandler() {
		return messageDialogHandler;
	}

	public IMessageDialog getErrorDialogHandler() {
		return errorDialogHandler;
	}

	public void setErrorDialogHandler(IMessageDialog errorDialogHandler) {
		this.errorDialogHandler = errorDialogHandler;
	}

	public String messageDialogButtonOk() {
		this.setButtonClickedMessage("application.menu.feedback.popupDialog.messageDialog.customHandler.result.ok");
		this.messageDialog.setShow(false);
		return null;
	}

	public String errorDialogButtonOk() {
		this.setButtonClickedMessage("application.menu.feedback.popupDialog.errorDialog.customHandler.result.ok");
		this.errorDialog.setShow(false);
		return null;
	}
}
